var express = require("express");
var router = express.Router();
var config = require("../../config.json");
var Sequelize = require("sequelize");
var sequelize = new Sequelize(config.database, config.userName, config.password, {
	dialect: config.dialect,
	logging: false
});
var Polylines = sequelize.import("../../models/polyline");
var Pictures = sequelize.import("../../models/picture");

// 获取所有的多段线
router.get("/polylines", function (req, res, next) {
	sequelize.sync().then(function () {
		return Polylines.findAll();
	}).then(function (value) {
		res.writeHead(200);
		res.end(JSON.stringify(value));
	}).catch(function (error) {
		res.writeHead(500);
		res.end(JSON.stringify(error));
	});
});

// 获取某多段线下的照片
router.get("/polylines/:id", function (req, res, next) {
	sequelize.sync().then(function () {
		return Pictures.findAll({
			where: {
				polyline: req.params.id
			}
		});
	}).then(function (value) {
		res.writeHead(200);
		res.end(JSON.stringify(value));
	}).catch(function (error) {
		res.writeHead(500);
		res.end(JSON.stringify(error));
	});
});

module.exports = router;